package goods

import (
	"context"
	"github.com/gin-gonic/gin"
	"mxshop-api/goods-web/api"
	"mxshop-api/goods-web/global"
	"mxshop-api/goods-web/proto"
	"net/http"
	"strconv"
)

/*
*
商品列表
pmin 最低价格
pmax 最高价格
ih   isHot
in   isNew
it   isTab
c    catetoryId
p    page //页码
pnum pagePerNums //每页数量
q    keywords
b    brandId
*/
func List(ctx *gin.Context) {
	request := proto.GoodsFilterRequest{}
	//最低价格
	pmin := ctx.DefaultQuery("pmin", "0")
	pminInt, _ := strconv.Atoi(pmin)
	request.PriceMin = int32(pminInt)
	//最高价格
	pmax := ctx.DefaultQuery("pmax", "0")
	pmaxInt, _ := strconv.Atoi(pmax)
	request.PriceMax = int32(pmaxInt)
	//关键字
	keywords := ctx.DefaultQuery("q", "")
	request.KeyWords = keywords
	//页码
	pageNum := ctx.DefaultQuery("p", "1")
	pageNumInt, _ := strconv.Atoi(pageNum)
	request.Pages = int32(pageNumInt)
	//每页数量
	pageSize := ctx.DefaultQuery("pnum", "20")
	pageSizeInt, _ := strconv.Atoi(pageSize)
	request.PagePerNums = int32(pageSizeInt)
	//分类
	categoryId := ctx.DefaultQuery("c", "0")
	categoryIdInt, _ := strconv.Atoi(categoryId)
	request.TopCategory = int32(categoryIdInt)
	//品牌
	brandId := ctx.DefaultQuery("b", "0")
	brandIdInt, _ := strconv.Atoi(brandId)
	request.Brand = int32(brandIdInt)
	//isHot
	isHot := ctx.DefaultQuery("ih", "0")
	if isHot == "1" {
		request.IsHot = true
	}
	isTab := ctx.DefaultQuery("it", "0")
	if isTab == "1" {
		request.IsTab = true
	}
	isNew := ctx.DefaultQuery("in", "0")
	if isNew == "1" {
		request.IsNew = true
	}
	//goodsList, err := global.GoodsSrvClient.GoodsList(ctx, &request)
	//添加链路追踪
	goodsList, err := global.GoodsSrvClient.GoodsList(context.WithValue(context.Background(), "ginContext", ctx), &request)
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	resMap := map[string]interface{}{
		"total": goodsList.Total,
	}
	resultList := make([]interface{}, 0)
	for _, value := range goodsList.Data {
		resultList = append(resultList, map[string]interface{}{
			"id":          value.Id,
			"name":        value.Name,
			"goods_brief": value.GoodsBrief,
			"desc":        value.GoodsDesc,
			"ship_free":   value.ShipFree,
			"images":      value.Images,
			"desc_images": value.DescImages,
			"front_image": value.GoodsFrontImage,
			"shop_price":  value.ShopPrice,
			"is_hot":      value.IsHot,
			"is_new":      value.IsNew,
			"on_sale":     value.OnSale,
			"category": map[string]interface{}{
				"id":   value.Category.Id,
				"name": value.Category.Name,
			},
			"brand": map[string]interface{}{
				"id":   value.Brand.Id,
				"name": value.Brand.Name,
			},
		})
	}
	resMap["resultList"] = resultList
	ctx.JSON(http.StatusOK, resMap)
}

/*
*
创建商品
*/
func New(ctx *gin.Context) {

}

/*
*
商品详情
*/
func Detail(ctx *gin.Context) {

}

/*
*
更新商品
*/
func Update(ctx *gin.Context) {

}

/*
*
删除商品
*/
func Delete(ctx *gin.Context) {

}
